﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using WebChatApi.Common.Interface;

namespace WebChatApi.Common
{
    /// <summary>
    /// Class for short link
    /// </summary>
    [DataContract]
    [KnownType(typeof(Uri))]
    public class ShortLink : IIdentifiable
    {
        #region Properties

        /// <summary>
        /// Gets the key.
        /// </summary>
        /// <value>
        /// The key.
        /// </value>
        [DataMember]
        public Guid Key { get; set; }

        /// <summary>
        /// Gets or sets the short link code.
        /// </summary>
        /// <value>
        /// The short link code.
        /// </value>
        [DataMember]
        public string ShortLinkCode { get; set; }

        /// <summary>
        /// Gets or sets the original URL.
        /// </summary>
        /// <value>
        /// The original URL.
        /// </value>
        [DataMember]
        public string OriginalUrl { get; set; }

        /// <summary>
        /// Gets or sets the created stamp.
        /// </summary>
        /// <value>
        /// The created stamp.
        /// </value>
        [DataMember]
        public DateTime CreatedStamp { get; set; }

        #endregion

        #region Constructor

        /// <summary>
        /// Initializes a new instance of the <see cref="ShortLink" /> class.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="createdStamp">The created stamp.</param>
        public ShortLink(Guid key, DateTime createdStamp)
        {
            this.CreatedStamp = createdStamp;
            this.Key = key;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ShortLink"/> class.
        /// </summary>
        public ShortLink()
            : this(Guid.NewGuid(), DateTime.UtcNow)
        {
        }

        #endregion

        /// <summary>
        /// Generates the short urls.
        /// </summary>
        /// <returns></returns>
        public string[] GenerateShortUrls()
        {
            return this.OriginalUrl == null ? new string[4] : GenerateShortUrls(this.OriginalUrl.ToString());
        }

        /// <summary>
        /// Generates the short urls.
        /// </summary>
        /// <param name="url">The URL.</param>
        /// <returns>String array for 4 codes.</returns>
        public static string[] GenerateShortUrls(string url)
        {
            string key = DateTime.UtcNow.GetHashCode().ToString();
            string[] chars = new string[]{
                "a","b","c","d","e","f","g","h",
                "i","j","k","l","m","n","o","p",
                "q","r","s","t","u","v","w","x",
                "y","z","0","1","2","3","4","5",
                "6","7","8","9","A","B","C","D",
                "E","F","G","H","I","J","K","L",
                "M","N","O","P","Q","R","S","T",
                "U","V","W","X","Y","Z"
              };
            string hex = EncodingUtil.Instance.EncryptToMD5(key + url);

            string[] result = new string[4];

            for (int i = 0; i < 4; i++)
            {
                int hexInt = 0x3FFFFFFF & Convert.ToInt32("0x" + hex.Substring(i * 8, 8), 16);
                string outChars = string.Empty;
                for (int j = 0; j < 6; j++)
                {
                    int index = 0x0000003D & hexInt;
                    outChars += chars[index];
                    hexInt = hexInt >> 5;
                }
                result[i] = outChars;
            }

            return result;
        }
    }
}
